﻿//********************************************************************************//
//创建日期：<创建日期>
//创建作者：<朱大鹏>
//功能说明：来函信息查询
//LAST MODIFY BY GUODENGJIE AT 20170816
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using DevExpress.Web;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration;
using Web.Class;
using System.Web.UI;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class LaiHanXXCX : BasePage
    {
        #region 属性
        public DataTable MyDataSource
        {
            get { return ViewState["MyDataSource"] as DataTable; }
            set { ViewState["MyDataSource"] = value; }
        }
        #endregion

        #region 变量
        /// <summary>
        /// 数据库访问
        /// </summary>
        SpHelper _sphelper = new SpHelper();
        /// <summary>
        /// 访问管理块信息
        /// </summary>
        YS_JX_GUANLIKBLL glkbll = new YS_JX_GUANLIKBLL();

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(gvList.SettingsPager.Mode+"1");
            
            //Response.Write(gvList.SettingsPager.Mode);
            if (!IsPostBack)
            {
                InitControl();
                MyDataSource = null;
            }
            gvList.DataSource = MyDataSource;
            gvList.DataBind();
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            BindData();

            //调用前端方法 loadColor
            ScriptManager.RegisterStartupScript(this, typeof(UpdatePanel), "", "loadNumber();", true);
        }
        /// <summary>
        /// 根据站点加载管理块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmZhanDian_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmGuanLiK.Items.Clear();
            YS_JX_GUANLIKBLL glkbll = new YS_JX_GUANLIKBLL();
            string zhandianBH = cmZhanDian.Value.ToString();

            DataTable glkdt = glkbll.SelectYS_JX_GUANLIKsDynamic(" I_JLZT=0 and (S_ZHANDIAN='" + zhandianBH + "' or '00'='" + zhandianBH + "')", " S_ZHanDIan");

            cmGuanLiK.DataSource = glkdt;
            cmGuanLiK.TextField = "S_GUANLIKMC";
            cmGuanLiK.ValueField = "S_GUANLIKH";
            cmGuanLiK.DataBind();
            
            cmGuanLiK.Items.Insert(0, new DevExpress.Web.ListEditItem("全部", "0"));
            
            cmGuanLiK.SelectedIndex = 0;
        }

        protected void BtnToExcel_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.WriteXlsxToResponse();
        }
        //20190326 添加来函删除功能 add begin
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            int id = Convert.ToInt32(this.HiddenView.Get("ID"));//主键ID
            string userid = LoginInfo.CurrentUser.UserID.ToString();

            if (e.Parameter == "del")//删除
            {
                DataSet ds = new DataSet();
                ds = _sphelper.P_LH_DELLAIHANXX(id, userid);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["V_I_RESULT"].ToString() == "0")
                    {
                        cr.IsSuccess = true;
                    }
                    else
                    {
                        cr.IsSuccess = false;
                    }
                    cr.IsAlertMsg = true;
                    cr.Msg = ds.Tables[0].Rows[0]["V_S_MESSAGE"].ToString();
                }
                else
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    cr.Msg = "删除失败。";
                }
                e.Result = cr.ToString();
                return;
            }
        }
        /// <summary>
        /// 客户端刷新list
        /// </summary>
        protected void grid_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            BindData();
            //调用前端方法 loadColor
            ScriptManager.RegisterStartupScript(this, typeof(UpdatePanel), "", "loadColor();", true);
        }
        //20190326 添加来函删除功能 add end
        #endregion

        #region 方法
        /// <summary>
        /// 绑定查询控件
        /// </summary>
        private void InitControl()
        {
            gvList.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //绑定来函类型
            DataSet ds = _sphelper.P_YS_LH_LAIHANLX(BusinessTool.WType);
            DataRow dr = ds.Tables[0].NewRow();
            dr["I_LAIHANH"] = "0";
            dr["S_LAIHANLXMC"] = " ";
            ds.Tables[0].Rows.InsertAt(dr,0);

            cmLaiHanLX.DataSource = ds;
            cmLaiHanLX.TextField = "S_LAIHANLXMC";
            cmLaiHanLX.ValueField = "I_LAIHANH";
            cmLaiHanLX.DataBind();
            cmLaiHanLX.SelectedIndex = 0;

            //加载站点
            if (BusinessTool.WType == 0)
            {
                string zhandianBH = BusinessTool.ZHANDIANBH();
                BLL.YS_JX_ZHANDIANBLL bllT = new YS_JX_ZHANDIANBLL();
                string where = "";
                DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANBH", "ROLEID", null, string.Format("USERID={0}", LoginInfo.CurrentUser.UserID)).Tables[0];
                string zhandian = dtZhanDian.Rows[0]["TEXT"].ToString();//站点
                string roleid = dtZhanDian.Rows[0]["VALUE"].ToString();//9001 业务员，3001 办事处主任
                if (Convert.ToInt32(roleid) > 3000)
                {
                    where = " S_ZHANDIANBH='" + zhandianBH + "' or '00'='" + zhandianBH + "'";

                }
                DataTable rs = bllT.SelectYS_JX_ZHANDIANsDynamic(where, null);

                this.cmZhanDian.DataSource = rs;
                this.cmZhanDian.TextField = "S_ZHANDIANMC";
                this.cmZhanDian.ValueField = "S_ZHANDIANBH";
                this.cmZhanDian.DataBind();
                if (zhandianBH == "00")
                {
                    //this.cmZhanDian.Items.Insert(0, new ListEditItem("00-中心", "00"));
                }
                else
                {
                    if (Convert.ToInt32(roleid) > 3000)
                    {

                        cmZhanDian.ReadOnly = true;
                    }
                }
                this.cmZhanDian.SelectedIndex = 0;
                if (zhandianBH.Trim().Length > 0)
                {
                    cmZhanDian.Value = zhandianBH;
                }

                YS_JX_GUANLIKBLL glkbll = new YS_JX_GUANLIKBLL();
                zhandianBH = cmZhanDian.Value.ToString();

                DataTable glkdt = glkbll.SelectYS_JX_GUANLIKsDynamic(" I_JLZT=0 and (S_ZHANDIAN='" + zhandianBH + "' or '00'='" + zhandianBH + "')", " S_ZHanDIan");

                cmGuanLiK.DataSource = glkdt;
                cmGuanLiK.TextField = "S_GUANLIKMC";
                cmGuanLiK.ValueField = "S_GUANLIKH";
                cmGuanLiK.DataBind();
                cmGuanLiK.Items.Insert(0, new DevExpress.Web.ListEditItem("全部", "0"));

                cmGuanLiK.SelectedIndex = 0;
            }
            else
            {
                this.tdTYSPBM1.Visible = false;
                this.tdTYSPBM2.Visible = false;
                this.gvList.Columns[3].Visible = false;
                this.gvList.Columns[4].Visible = false;
                //步骤去除节水科,主任换为取水科长
                this.cmLiuChengBH.Items.RemoveAt(6);
                this.cmLiuChengBH.Items[5].Text = "取水科长";
            }
            //加载登记日期条件
            int y = DateTime.Now.Year;
            this.cmbSY1.Items.Add(" ",null);
            //this.cmbSY2.Items.Add(" ",null);
            for (int i = y; i>=y-20; i--)
			{
			    this.cmbSY1.Items.Add(i.ToString(),i.ToString());
                //this.cmbSY2.Items.Add(i.ToString(),i.ToString());
			}
            this.cmbSY1.SelectedIndex = 1;
            this.cmbSM1.Items.Add(" ", null);
            //this.cmbSM2.Items.Add(" ", null);
            for (int i = 1; i <= 12; i++)
            {
                this.cmbSM1.Items.Add(i.ToString(), i.ToString());
                //this.cmbSM2.Items.Add(i.ToString(), i.ToString());
            }
            //20190326 add 添加来函特殊权限点删除功能
            int flag = GetLaiHanPermission();
            if (flag == 0)
            {
                gvList.Columns[18].Visible = false;
            }
            //可修改来函文件
            //HiddenView.Set("LHFlag", flag);
            try
            {
                string permission3 = ConfigurationManager.AppSettings["LHXGPermission3"].ToString();
                if (Platform.Permission.PermissionCheck.CheckUserPermission(LoginInfo.CurrentUser.UserID, permission3))
                    HiddenView.Set("LHFlag", 1);
                else
                    HiddenView.Set("LHFlag", 0);
            }
            catch { }
        }

        /// <summary>
        /// 查询用户是否有指定特殊权限点
        /// </summary>
        private int GetLaiHanPermission()
        {
            int _cur_userID = 0;
            int _flag = 0;
            string permission1 = "";//基本权限，可删当天来函
            string permission2 = "";//2级权限，可删除流程中来函
            try
            {
                _cur_userID = LoginInfo.CurrentUser.UserID;
                permission1 = ConfigurationManager.AppSettings["LHXGPermission1"].ToString();
                permission2 = ConfigurationManager.AppSettings["LHXGPermission2"].ToString();
                if (Platform.Permission.PermissionCheck.CheckUserPermission(_cur_userID, permission1))
                {
                    _flag = 1;  
                }
                if (Platform.Permission.PermissionCheck.CheckUserPermission(_cur_userID, permission2))
                {
                    _flag = 2;
                }
            }
            catch
            {
            }
            return _flag;
        }
        
        public static IList ListTypeForEnum<T>(ASPxComboBox c)
        {
            ArrayList list = new ArrayList();
            int index = 0;
            foreach (int i in Enum.GetValues(typeof(T)))
            {

                ListEditItem listitem = new ListEditItem(Enum.GetName(typeof(T), i), i.ToString());
                c.Items.Insert(index,listitem);
                index++;
            }
            return list;
        }
        /// <summary>
        /// 绑定gvList数据
        /// </summary>
        private void BindData()
        {
            //string zhandian = ;
            //string glk = cmGuanLiK.SelectedIndex<=0?null:Convert.ToString(cmGuanLiK.Value);
            int laihanlx=Convert.ToInt32(cmLaiHanLX.Value);
            //全部则设为-1
            //laihanlx = laihanlx == 0 ? -1 : laihanlx;
            //int liuchengbh=Convert.ToInt32(cmLiuChengBH.Value); 
           // int liuchengzt=Convert.ToInt32(cmShenPiJG.Value);

            string djy=null;
            string djm=null;
            if (this.cmbSY1.SelectedIndex > 0)
            {
                djy = Convert.ToString(this.cmbSY1.Value);
            }
            if (this.cmbSM1.SelectedIndex > 0)
            {
                djm = Convert.ToString(this.cmbSM1.Value);
            }
            //string dengjirq1=null;       
            //string dengjirq2=null;
            //if (this.cmbSY1.SelectedIndex > 0)
            //{
            //    if (this.cmbSM1.SelectedIndex == 0)
            //    {
            //        dengjirq1 = this.cmbSY1.Value.ToString() + "-01-01";
            //    }
            //    else
            //    {
            //        dengjirq1 = this.cmbSY1.Value.ToString() + "-" + this.cmbSM1.Value.ToString() + "-01";
            //    }
            //}            
            //if (this.cmbSY2.SelectedIndex > 0)
            //{
            //    if (this.cmbSM2.SelectedIndex == 0)
            //    {
            //        dengjirq2 = this.cmbSY2.Value.ToString() + "-01-01";
            //    }
            //    else
            //    {
            //        dengjirq2 = this.cmbSY2.Value.ToString() + "-" + this.cmbSM2.Value.ToString() + "-01";
            //    }
            //}
            string yonghuh="0";
            string laihanbh="0";
            int y=0;
            int m=0;
            if (txtShuiLiangNY.Text=="")
            {
                y = 0;
                m = 0;
            }
            else if (txtShuiLiangNY.Text.Length == 6)
            {
                y = Convert.ToInt32(txtShuiLiangNY.Text) / 100;
                m = Convert.ToInt32(txtShuiLiangNY.Text) % 100;
            }
            else 
            {
                y = Convert.ToInt32(txtShuiLiangNY.Text);
                m = 0;
            }
            if (txtLaiHanBH.Text.Trim().Length > 0)
            {
                laihanbh = txtLaiHanBH.Text;
            }
            if (txtYongHuH.Text.Trim().Length > 0)
            {
                yonghuh = txtYongHuH.Text;
            }
            DataSet ds = _sphelper.P_YS_CX_LAIHANXXCX(BusinessTool.WType,
                Convert.ToString(cmZhanDian.Value)=="00"?null:Convert.ToString(cmZhanDian.Value),
                cmGuanLiK.SelectedIndex <= 0 ? null : Convert.ToString(cmGuanLiK.Value),
                cmLaiHanLX.SelectedIndex <= 0 ? null :(int?)Convert.ToInt32(cmLaiHanLX.Value),
                cmLiuChengBH.SelectedIndex <= 0 ? null : (int?)Convert.ToInt32(cmLiuChengBH.Value),
                cmShenPiJG.SelectedIndex <= 0 ? null : (int?)Convert.ToInt32(cmShenPiJG.Value),
                txtYongHuH.Text, 
                this.txtLaiHanBH.Text,
                this.txtShuiLiangNY.Text.Trim() == "" ? null : (int?)Convert.ToInt32(this.txtShuiLiangNY.Text.Trim())/100,
                this.txtShuiLiangNY.Text.Trim() == "" ? null : (int?)Convert.ToInt32(this.txtShuiLiangNY.Text.Trim()) % 100, 
                LoginInfo.CurrentUser.UserID, 
                txtShenPiBM.Text.Trim(),
                djy,
                djm);
            MyDataSource = null;
           
            MyDataSource = ds.Tables[0];
           
            gvList.DataSource = MyDataSource;
            gvList.DataBind();
            if (ds!=null&&ds.Tables.Count>0&&ds.Tables[0] != null)
            {
                PageCount.InnerText = "共" + ds.Tables[0].Rows.Count + "条";
            }
        }
        /// <summary>
        /// 获得业务员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetYeWuYuanXM(string id)
        {
            if (id.Trim().Length == 0)
            {
                return "";
            }
            string name = "";
            DataTable user = BusinessTool.GetTextAndValue("view_as_userinfo", "UserName", "USERID", null, string.Format("USERID={0}", id)).Tables[0];
            if (user != null && user.Rows.Count > 0)
            {
                name = user.Rows[0]["Text"].ToString();
            }
            return name;
        }
        /// <summary>
        /// 将可序列化对象转成Byte数组
        /// </summary>
        /// <param name="o">对象</param>
        /// <returns>返回相关数组</returns>
        protected static byte[] ObjectToByteArray(object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, o);
            ms.Close();
            return ms.ToArray();
        }
        /// <summary>
        /// 将可序列化对象转成的byte数组还原为对象
        /// </summary>
        /// <param name="b">byte数组</param>
        /// <returns>相关对象</returns>
        protected static object ByteArrayToObject(byte[] b)
        {
            MemoryStream ms = new MemoryStream(b, 0, b.Length);
            BinaryFormatter bf = new BinaryFormatter();
            return bf.Deserialize(ms);
        }

        #endregion
    }
}

// RowDblClick="function(s,e){s.GetRowValues(e.visibleIndex, 'S_LAIHANBH;S_YONGHUBH;I_LAIHANLX;ID', ShowMag);}"